package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.basic.BasicPojo;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.pojo.Store;
import com.itheima.restkeeper.mapper.StoreMapper;
import com.itheima.restkeeper.req.StoreVo;
import com.itheima.restkeeper.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description：门店信息账号 服务实现类
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Override
    public Page<Store> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) {
        //分页条件构建
        Page<Store> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Store> wrapper = Wrappers.<Store>lambdaQuery();
        //按门店名称查询
        if (!EmptyUtil.isNullOrEmpty(storeVo.getStoreName())) {
            wrapper.like(Store::getStoreName, storeVo.getStoreName());
        }
        //按门店状态查询
        if (!EmptyUtil.isNullOrEmpty(storeVo.getEnableFlag())) {
            wrapper.eq(Store::getEnableFlag, storeVo.getEnableFlag());
        }
        return page(page, wrapper);
    }

    @Override
    public Store createStore(StoreVo storeVo) {
        Store store = BeanConv.toBean(storeVo, Store.class);
        save(store);
        return store;
    }

    @Override
    public Boolean updateStore(StoreVo storeVo) {
        return updateById(BeanConv.toBean(storeVo,Store.class));
    }

    @Override
    public Boolean deleteStore(String[] checkedIds) {
        List<Long> list = Stream.of(checkedIds).map(Long::valueOf).collect(Collectors.toList());
        return removeByIds(list);
    }

    @Override
    public List<Store> findStoreVoList() {
        LambdaQueryWrapper<Store> wrapper = Wrappers.<Store>lambdaQuery();
        wrapper.eq(BasicPojo::getEnableFlag,SuperConstant.YES);
        return list(wrapper);
    }

}
